package com.ayu.a.practice02;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author 徐林玉
 * @Description CountAndSayPrac
 * @Date 2022/11/15 14:18
 * @Version 1.0
 */
public class CountAndSayPrac {

    public static void main(String[] args) {

        String s = countAndSayHelper(3, 4, "21");

        System.out.println("s = " + s);

    }


    public static String countAndSayHelper(int n, int max, String say) {

        if (n == max) {

            return say;
        }

        char[] c = say.toCharArray();

//        Map<Character, Integer> map = new LinkedHashMap<>();
//
//        for (int i = 0; i < c.length; i++) {
//            char tar = c[i];
//            Integer count = map.get(tar);
//            map.put(tar, count == null ? 1 : count + 1);
//        }
//
//        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
//
//
//        String s = "";
//        for (Map.Entry<Character, Integer> e :
//                entries) {
//
//            Character key = e.getKey();
//            Integer value = e.getValue();
//            s = s + value + key;
//        }


        int[] b = new int[c.length];
        int index = 0;

        for (int i = 0; i < c.length-1; i++) {

            if(c[i]!=c[i+1]){

                b[index++] = i;
            }
        }
        int value = 0;
        char key = ' ';
        String s = "";

        for (int i = 0; i < b.length; i++) {

            if(i==0){

                value = b[i];
                key = c[value];

            }else{
                value = b[i] - b[i - 1];

                key  = c[value];
            }

            s = s + value + key;

        }



        return countAndSayHelper(++n, max, s);
    }
}
